package View;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;

import Model.ListSlideModel;


import kdiapo.Main;
import kdiapo.Resizable;
import kdiapo.SlideShow;
import kdiapo.CreationFrame;


public class ImageView extends ElementView {
	private String pathValue_;
	private Resizable resizer_;
	private BufferedImage image_;
	
	public ImageView(CreationFrame mainFrame, int num,String path,int x,int y, int width, int height){
		super(mainFrame,num);
		
		this.pathValue_ = path;				
	    this.setBackground(Color.white);
	    try {
			image_ = ImageIO.read(new File(this.pathValue_));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    this.resizer_ = new Resizable(this);
	    this.resizer_.setBounds(x, y, width, height);
	    
	    addListeners();
	}
	
	public ImageView(CreationFrame mainFrame, int num, String path){
		super(mainFrame,num);
		
		this.pathValue_ = path;
		
		try {
			image_ = ImageIO.read(new File(this.pathValue_));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    this.setBackground(Color.white);

	    this.resizer_ = new Resizable(this);
	   
	    this.resizer_.setBounds(100, 100, image_.getWidth()+17, image_.getHeight()+17);
	      
	    //Initialisation des valeurs de l'element texte
	    ListSlideModel md = mainFrame.getModel();
	    md.setPosition(num, 100,  100,  image_.getWidth(), image_.getHeight());
	    
	    addListeners();
	}
	
	public ImageView(SlideShow mainFrame, int num, String path, int x, int y, int width, int height){
	
		super(num);
		
		this.pathValue_ = path;
		
		try {
			image_ = ImageIO.read(new File(this.pathValue_));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setBackground(Color.white);

	    this.resizer_ = new Resizable(this);
	    this.resizer_.setBounds(x, y, width, height);
	    
	    //addListeners();
	}
	
	public void removeElement(int num){
		getMainFrame().removeElement(num);
		//Mise � jour du mod�le
		getMainFrame().getModel().removeElement(num);
	}
	
	public void addListeners(){
		
		this.resizer_.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {}			
			public void keyReleased(KeyEvent e) {
				
				if(e.getKeyCode() == KeyEvent.VK_DELETE){
					removeElement(getNumElement());
				}
			}
			
			
			public void keyPressed(KeyEvent e) {}
		});
		
	    this.resizer_.addComponentListener(new ComponentListener() {
			public void componentShown(ComponentEvent e) {}
			public void componentResized(ComponentEvent e) {
				//System.out.println("largeur="+imageIcon.getIconWidth()+"\tlongueur="+imageIcon.getIconHeight());
				
				getMainFrame().getModel().setPosition(getNumElement(), 
						getMainFrame().getCurrentSlide().getComponent(getNumElement()).getX(),
						getMainFrame().getCurrentSlide().getComponent(getNumElement()).getY(), 
						resizer_.getWidth(),
						resizer_.getHeight());
		
			}
			public void componentMoved(ComponentEvent e) {				
				System.out.println("X="+getMainFrame().getCurrentSlide().getComponent(getNumElement()).getX()
						+"\tY="+getMainFrame().getCurrentSlide().getComponent(getNumElement()).getX());
				getMainFrame().getModel().setPosition(getNumElement(), 
						getMainFrame().getCurrentSlide().getComponent(getNumElement()).getX(),
						getMainFrame().getCurrentSlide().getComponent(getNumElement()).getY(), 
						resizer_.getWidth(),
						resizer_.getHeight());
			}
			public void componentHidden(ComponentEvent e) {}
		});
	    this.resizer_.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger())
		            doPop(e);	
			}
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger())
		            doPop(e);
			}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			
			public void mouseClicked(MouseEvent e) {}
			public void doPop(MouseEvent e){
				JPopupMenu popMenu = new JPopupMenu();
				JMenuItem anItem =  new JMenuItem("Remove this element");
				anItem.setIcon(new ImageIcon(Main.class.getResource("/remove_icon.png")));
				anItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//Suppression de l'�l�ment de la vue
						removeElement(getNumElement());
					}
				});
				
				popMenu.add(anItem);
				popMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}

	public String getPathValue() {
		return pathValue_;
	}

	public void setPathValue(String pathValue) {
		this.pathValue_ = pathValue;
	}


	public Resizable getResizer() {
		return resizer_;
	}

	public void setResizer(Resizable resizer) {
		this.resizer_ = resizer;
	}
	
	protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int x = 0;
        int y = 0;
        g.drawImage(resize(image_,resizer_.getWidth()-17,resizer_.getHeight()-17), x, y, this);
        
    }
	
	public static BufferedImage resize(BufferedImage img, int newW, int newH) {  
        int w = img.getWidth();  
        int h = img.getHeight();  
        System.out.println("Type : "+img.getType());
        BufferedImage dimg = new BufferedImage(newW, newH, img.getType());  
        Graphics2D g = dimg.createGraphics();  
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
        g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
        g.dispose();  
        return dimg;  
    }  
}
